package net.nagaseyasuhito.kinsenka.wicket.component;

import net.nagaseyasuhito.kinsenka.persist.entity.Task;
import net.nagaseyasuhito.kinsenka.wicket.page.ViewTaskPage;

import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;

public class TaskColumn extends AbstractColumn<Task> {
	private static class TaskColumnPanel extends Panel {
		private static final long serialVersionUID = 1L;

		private Task task;

		public TaskColumnPanel(String id, Task task) {
			super(id);
			this.setDefaultModel(new CompoundPropertyModel<TaskColumnPanel>(this));
			this.task = task;

			Link<Void> link = new Link<Void>("link") {
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick() {
					this.setResponsePage(new ViewTaskPage(TaskColumnPanel.this.task));
				}
			};
			link.add(new AbbreviateLabel("task.subject", 16));
			link.add(new RelativeDateLabel("task.updateTimestamp"));
			link.add(new AbbreviateLabel("task.detail", 24));

			this.add(link);
		}
	}

	private static final long serialVersionUID = 1L;

	public TaskColumn() {
		super(new ResourceModel("tasks"));
	}

	@Override
	public void populateItem(Item<ICellPopulator<Task>> cellItem, String componentId, IModel<Task> rowModel) {
		cellItem.add(new TaskColumnPanel(componentId, rowModel.getObject()));
	}
}
